<?php
class PortalWebServiceClient extends WebServiceClient {
	private $timeless;
	private $time_now;
	private $time_shift;
	const WEB_SERVICE_CONFIG_FILE='/data/mytvplus/portal_service/conf/client.xml';
	
	public function __construct($timeless = FALSE, $time_now = NULL, $time_shift = FALSE) {
		$webservice_config = ConfigReaderFactory::getInstance()->getConfigReader(PortalWebServiceClient::WEB_SERVICE_CONFIG_FILE)->getConfig();
		$ws_url = (string) $webservice_config->url;
		$ws_key = (string) $webservice_config->key;
		$ws_ver = (string) $webservice_config->version;
		$this->timeless = $timeless;
		$this->time_now = is_null($time_now) ? time() : $time_now;
		$this->time_shift = $time_shift;
		parent::__construct($ws_url, $ws_key, $ws_ver, TRUE);
	}

	public function setTimeless($timeless) {
		$this->timeless = $timeless;
	}
	
	public function setTimeNow($time_now) {
		$this->time_now = $time_now;
	}

	public function get($name, array $arguments=array()) {
		$options = array(
			"timeless" => $this->timeless,
			"time_now" => $this->time_now,
			"time_shift" => $this->time_shift
		);
		
		for ($i=0; $i<5; $i++) { 
			try {
				$value =  $this->invoke($name, $arguments, array(), $options);
				if ( !(is_array($value) && !(empty($value)))) {
					error_log('debug_log: '. __CLASS__  .'->' . __FUNCTION__ . '-> ' . $name . '-> failed attempt:' .$i . '$RET: ' . var_export($value,true) . ' | $ARG:' . var_export($arguments, true)); 
					if ($i<4) continue;
				}
				
				return $value;
			} catch (HttpInvalidParamException $e) {
				error_log('debug_log_catched_error: '. __CLASS__  .'->' . __FUNCTION__ . '-> ' . $name . '-> failed attempt:' .$i ); 
				//error_log('debug_log_error: '. __CLASS__  .'->' . __FUNCTION__ . '-> ' . $name . '-> error:'. var_export($e,true)); 
				error_log('sleep!!!');
				sleep(1);
			}
		}
		error_log('Failed calling web service server!!!');
		return array();
	}
}
?>
